//
//  TaskList.m
//  QiYReader
//
//  Created by 奇阅科技 on 14/12/20.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "TaskList.h"
#import "PathHelper.h"
#import "UserAccount.h"
//static TaskList *taskList;
@implementation TaskList
+ (NSString *)getAccountsStoragePath {
    UserAccount *user = [UserAccount currentAccount];
    NSString *uid = @"love";
    if (user) {
        uid = user.uid;
    }
    return [[PathHelper documentDirectoryPathWithName:@"db"]
            stringByAppendingPathComponent:[NSString stringWithFormat:@"%@TaskList",uid]];
}

+ (void)saveAccount:(TaskList *)taskList{
    [NSKeyedArchiver archiveRootObject:taskList toFile:[self getAccountsStoragePath]];
}
-(instancetype)init{
    self = [super init];
    if (self) {
        UserAccount *user = [UserAccount currentAccount];
        NSString *uid = @"love";
        if (user) {
            uid = user.uid;
        }
        self.uid = uid;
        self.time = [self dateStr];
        NSMutableArray *taskArrays = [NSMutableArray new];
        NSArray *titleArray = @[@"发15字以上书评",@"去书城逛逛",@"加入一本书到书架",@"分享一本书",@"赞一条书评",@"收藏一本书"];
        NSArray *expArray = @[@"10",@"10",@"10",@"20",@"5",@"5"];
        for (int i = 0; i<6; i++) {
            Task *task = [Task new];
            task.title = titleArray[i];
            task.exp = expArray[i];
            task.integral = expArray[i];
            task.isFinshed = NO;
            [taskArrays addObject:task];
        }
        self.taskArray = taskArrays;
        [TaskList saveAccount:self];
    }
    return self;
}
+ (TaskList *)currentAccount {
    @synchronized(self) {
        TaskList *taskList = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getAccountsStoragePath]];
        if (!taskList) {
            taskList = [TaskList new];
        }else{
            if (![taskList.time isEqualToString:[taskList dateStr]]) {
                [taskList resetTaskList];
            }
        }
        return taskList;
    }
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (self != nil) {
        self.uid = [decoder decodeObjectForKey:@"uid"];
        self.time = [decoder decodeObjectForKey:@"time"];
        self.taskArray = [decoder decodeObjectForKey:@"taskArray"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.uid forKey:@"uid"];
    [encoder encodeObject:self.time forKey:@"time"];
    [encoder encodeObject:self.taskArray forKey:@"taskArray"];
}
//重置任务
-(void)resetTaskList{
    UserAccount *user = [UserAccount currentAccount];
    NSString *uid = @"love";
    if (user) {
        uid = user.uid;
    }
    self.uid = uid;
    self.time = [self dateStr];
    for (Task *task in self.taskArray) {
        task.isFinshed = NO;
    }
    [TaskList saveAccount:self];
}
-(NSString *)dateStr{
    static NSDateFormatter *dateFormatter;
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc]init];
        dateFormatter.dateFormat = @"yyyyMMdd";
    }
    NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
    return dateStr;
}
@end
